//
//  ViewController.m
//  XMLTest
//
//  Created by Adiljan on 2019/2/17.
//  Copyright © 2019年 Adiljan. All rights reserved.
//

#import "ViewController.h"
#import "TeaXMLModel.h"
#import "MessageViewController.h"

@interface ViewController ()<NSXMLParserDelegate>
@property (strong, nonatomic) NSMutableArray *teaArray;
@property (strong, nonatomic) TeaXMLModel *Tmodel;
@property (strong, nonatomic) NSString *stringData;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 365, 300)];
    [btn setTitle:@"翻页" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(clickMe:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    btn.backgroundColor = [UIColor blueColor];
    
    NSString *filePath =  [[NSBundle mainBundle] pathForResource:@"test" ofType:@"xml"];
    
    
    NSURL *url = [[NSURL alloc]initFileURLWithPath:filePath];
    NSXMLParser *par = [[NSXMLParser alloc]initWithContentsOfURL:url];
    par.delegate = self;
    [par parse];
}
-(void)clickMe:(UIButton *)sender{
    MessageViewController *mes = [[MessageViewController alloc] initWithNibName:@"MessageViewController" bundle:nil];
//    [self.navigationController pushViewController:mes animated:YES];
    [self presentViewController:mes animated:YES completion:nil];
}

- (NSMutableArray *)teaArray{
    if (!_teaArray) {
        _teaArray = [NSMutableArray array];
    }
    return _teaArray;
}

//2.-- NSXMLParserDelegate  代理方法
#pragma mark -- NSXMLParserDelegate

- (void)parserDidStartDocument:(NSXMLParser *)parser{
    NSLog(@"xml开始解析");
    
    //    self.teaArray = [NSMutableArray array];
    
}
- (void)parserDidEndDocument:(NSXMLParser *)parser{
    NSLog(@"结束解析");
    
    for (TeaXMLModel *model in self.teaArray) {
        NSLog(@"%@",model);
    }
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary<NSString *,NSString *> *)attributeDict{
    NSLog(@"开始标签：%@",elementName);
    
//    if ([elementName isEqualToString:@"teacher"]) {
//        self.Tmodel = [TeaXMLModel new];
//    }
    
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
    NSLog(@"结束标签：%@",elementName);
//    if ([elementName isEqualToString:@"name"]) {
//        self.Tmodel.name = self.stringData;
//    }else if ([elementName isEqualToString:@"age"]){
//        self.Tmodel.age = self.stringData;
//    }else if ([elementName isEqualToString:@"phone"]){
//        self.Tmodel.phone = self.stringData;
//    }else if ([elementName isEqualToString:@"teacher"]){
//        [self.teaArray addObject:self.Tmodel];  //当遇到结束标签 添加到数组里面
//    }
    NSLog(@"teaArray: %@",self.teaArray);
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
    NSLog(@"内容是：%@",string);
    self.stringData = string;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
